package com.estate.struts.actions;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import com.estate.business.LawsService;
import com.estate.utils.FileUtil;
import com.estate.utils.LawsUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.CompanyStuffVo;
import com.estate.vo.impl.LawsVo;
import com.estate.vo.impl.UserInfoVo;

public class LawsAction extends EstateSupport {

	private String id;
	private String title;
	private String content;
	private UserInfoVo postUser;
	private Date postDate;
	private int readNum;
	private String state;
	
	//添加字段
	private String fileSaveName;
	private String fileRealName;
	private File file;
	private String fileName;
	private String uploadUserId;
	private Date uploadDate;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private LawsService lawService;
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());

		String page = "";
		if("add".equals(this.getMethod())){
//			System.out.println("laws action file =========== " + this.getFileName());
//			System.out.println("laws action file =========== " + this.getFileName().getBytes("ISO-8859-1"));
//			System.out.println("laws action file =========== " + new String(this.getFileName().getBytes("ISO-8859-1"),"UTF-8"));
			LawsVo law = this.getAddLaws();
			if(law != null){
				boolean result = this.getLawService().addLaw(law,this.getFile());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
			
		}else if("list".equals(this.getMethod())){
			List<Vo> lawsList = this.getLawService().getAllLaws(this.getSessionUser());
			this.getRequest().setAttribute("lawsList", lawsList);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allLaws";
		}else if("detail".equals(this.getMethod())){
			Vo law = this.getLawService().getLaw(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("law", law);
			this.getLawService().updateReadNum((LawsVo) law);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo law = this.getLawService().getLaw(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("law", law);
			page = "update";
		}else if("update".equals(this.getMethod())){
			LawsVo law = this.getUpdateLaws();
			if(law == null){
				page = "fail";
			}else{
				boolean result = this.getLawService().updateLaw(law,this.getFile());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}else if("post".equals(this.getMethod())){
			this.getLawService().updatePost(Integer.parseInt(this.getId()));
			page = this.SUCCESS;
		}else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
				boolean result = this.getLawService().deleteMultiLaws(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					LawsVo law = new LawsVo();
					law.setId(Integer.parseInt(this.getId()));
					boolean result = this.getLawService().deleteLaw(law);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		return page;
	}
	
	private void setLawsFile(LawsVo law){
		if(this.getFileName() != null && !"".equals(this.getFileName())){
			if(this.getFile() != null){
				String saveName = FileUtil.getReplaceFileName(this.getFileName());
				String savePath = FileUtil.LAWS_FILE+saveName;
				
				law.setFilePath(savePath);
				try {
					law.setFileRealName(new String(this.getFileName().getBytes("ISO-8859-1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				law.setFileSaveName(saveName);
				law.setUploadDate(new Date());
				law.setUploadUser(this.getSessionUser());
			}
		}
	}
	
	private LawsVo getUpdateLaws(){
		LawsVo law = new LawsVo();

		law.setId(Integer.parseInt(this.getId()));
		law.setContent(this.getContent());
		int stateInt = Integer.parseInt(this.getState());
		law.setState(LawsUtil.getLawsString(stateInt));
//		law.setPostUser(this.getSessionUser());
		law.setTitle(this.getTitle());
		
		this.setLawsFile(law);
		
		return law;
	}
	private LawsVo getAddLaws(){
		LawsVo law = new LawsVo();
		
		law.setContent(this.getContent());
		int stateInt = Integer.parseInt(this.getState());
		law.setState(LawsUtil.getLawsString(stateInt));
		if(stateInt == 2){
			law.setPostDate(new Date());
			law.setReadNum(0);
		}
		law.setPostUser(this.getSessionUser());
		law.setTitle(this.getTitle());
		
		this.setLawsFile(law);
		
		return law;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public UserInfoVo getPostUser() {
		return postUser;
	}
	public void setPostUser(UserInfoVo postUser) {
		this.postUser = postUser;
	}
	public Date getPostDate() {
		return postDate;
	}
	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}
	public int getReadNum() {
		return readNum;
	}
	public void setReadNum(int readNum) {
		this.readNum = readNum;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public LawsService getLawService() {
		return lawService;
	}
	public void setLawService(LawsService lawService) {
		this.lawService = lawService;
	}
	public String getFileSaveName() {
		return fileSaveName;
	}
	public void setFileSaveName(String fileSaveName) {
		this.fileSaveName = fileSaveName;
	}
	public String getFileRealName() {
		return fileRealName;
	}
	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getUploadUserId() {
		return uploadUserId;
	}
	public void setUploadUserId(String uploadUserId) {
		this.uploadUserId = uploadUserId;
	}
	public Date getUploadDate() {
		return uploadDate;
	}
	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}
}
